package com.second.ssyt_test.assist.servlet;

import java.io.*; 
import java.lang.Runtime;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import com.second.ssyt_test.assist.dao.AssistDao;
import com.second.ssyt_test.assist.entity.AssistEntity;
import com.second.ssyt_test.common.Constant;


public class AssistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//防止乱码
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		String command = request.getParameter("command");
		 if ("list".equals(command)) {
			List(request,response);
		}else if ("backups".equals(command)) {
			backups(request,response);
		}else if ("addbackups".equals(command)) {
			addbackups(request,response);
		}else if ("restoration".equals(command)) {
			restoration(request,response);
		}else{
			
			request.getRequestDispatcher("/WEB-INF/view/common/error.jsp");
		}
	}
	/**
	 * 添加备份
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void addbackups(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
        //获取前台参数
          String backupsmemeo =  request.getParameter("backupsmemeo");
          
		//调用逻辑
	    AssistDao.addbackups(backupsmemeo);
		//转向
		request.getRequestDispatcher("/AssistServlet?command=list").forward(request, response);
				
		
	}

	/**
	 * 数据还原
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void restoration(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String filepath = "e:\\mysqlbackup\\yec.sql"; // 备份的路径地址 
		//新建数据库test 

		String stmt1 = "mysqladmin -uroot -proot create yec"; 

		String stmt2 = "mysql -uroot -proot yec < " + filepath; 
		String[] cmd = { "cmd", "/c", stmt2 }; 

		try { 
		Runtime.getRuntime().exec(stmt1); 
		Runtime.getRuntime().exec(cmd); 
		System.out.println("数据已从 " + filepath + " 导入到数据库中"); 
		} catch (IOException e) { 
		e.printStackTrace(); 
		} 
		request.getRequestDispatcher("AssistServlet?command=list").forward(request, response);
	}

	/**
	 * 数据备份
	 * @param request
	 * @param response
	 */
    private void backups(HttpServletRequest request,
			HttpServletResponse response) {
    	String user = "root"; // 数据库帐号
    	String password = "root"; // 登陆密码
    	String database = "yec"; // 需要备份的数据库名
    	String filepath = "e:\\mysqlbackup\\yec.sql"; // 备份的路径地址

    	String stmt1 = "mysqldump " + database + " -u " + user + " -p"
    	+ password + " --result-file=" + filepath;
    	try {
    	Runtime.getRuntime().exec(stmt1);
    	}
    	catch (IOException e) {
    	e.printStackTrace();
    	}
      

	}

	/**
     * 备份数据查询
     * @param request
     * @param response
     * @throws IOException 
     * @throws ServletException 
     */
	private void List(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取前台参数
		
		//调用逻辑
	    List<AssistEntity> assisEntity = AssistDao.list();
		request.setAttribute("assisEntity", assisEntity);
		//转向
		request.getRequestDispatcher("/WEB-INF/view/assist/index.jsp?").forward(request, response);
		
		
	}
}
